package com.example.assign2_list;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class UpdateCountry extends Activity {
	private String taskId;
	private EditText year;
	private EditText country;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_country);
        Intent i = getIntent();
        
        EditText updateYear = (EditText) findViewById(R.id.update_year);
        EditText updateCountry = (EditText) findViewById(R.id.update_country);
        
        taskId = i.getLongExtra("taskId", 0) + "";
        updateYear.setText(i.getIntExtra("year", 0) + "");
        updateCountry.setText(i.getStringExtra("country"));
    }
    
    public void addToList(View v){
    	year = (EditText) findViewById(R.id.update_year);
    	country = (EditText) findViewById(R.id.update_country);
    	
    	if(year.getText().length() == 0 || country.getText().length() == 0)
    	{
    		Toast.makeText(this, R.string.addError, Toast.LENGTH_SHORT).show();
    	}
    	else{    		
    		/*	Add information to db	*/
    		addNewToList(Long.parseLong(taskId));
    		System.out.println("Task with ID: " + taskId + " is updated");
    		finish();		//	Close window when done
    	}
    }
    
    private void addNewToList(long taskId){
		int yearInt = Integer.parseInt(year.getText().toString());
		String countryString = country.getText().toString();
		MyCountries.adapt.clear();
		MyCountries.dbDataSource.updateTask(taskId, yearInt, countryString);
		MyCountries.countryList = MyCountries.dbDataSource.getAllTasks();
		MyCountries.adapt.addAll(MyCountries.countryList);
    }
}
